var mockupbuild = require("../mockup");
var path = require('path');
var basename = path.basename(__filename, ".js");
var Mock = require('mockjs');
var Chance = require('chance');
var Random = Mock.Random;
var chance = new Chance();

chance.mixin({
    'pengyouquanuser': function () {
        return this.pickone([Random.cname(), Random.name()]);
    },
    'pengyouquansentence': function() {
        return this.pickone([Random.csentence(), Random.sentence()]);
    },
    'pengyouquancommoent': function() {
        let self = this;
        return self.pengyouquansentence();
    }
});

Function.prototype.auiClone = function() {
    var that = this;
    var temp = function temporary() { return that.apply(this, arguments); };
    for(var key in this) {
        if (this.hasOwnProperty(key)) {
            temp[key] = this[key];
        }
    }
    return temp;
};

function chooseone(obj, searched, findarr) {
    var isContainInSearched = findarr.every(function(v) {
        return searched.includes(v);
    });
    if (isContainInSearched) {
        let servied = chance.pickone(findarr);
        findarr.forEach(function(a) {
            if (a !== servied) {
                delete obj[a];
            }
        });
    }
    return isContainInSearched;
}

Mock.Handler._object = Mock.Handler.object.auiClone();
Mock.Handler.object = function(options) {
    let ret = null;
    if (options.name.indexOf("body") > -1) {
        ret = Mock.Handler._object(options);
        let keys = Object.keys(ret);

        // 处理text link
        if (keys.indexOf("text") > -1 || keys.indexOf("link") > -1) {
            if (keys.indexOf("text") > -1 && keys.indexOf("link") > -1) {
                let deleted = chance.pickone(["text", "link"]);
                delete ret[deleted];
            }
        } else {
            ret["text"] = chance.pengyouquansentence();
        }

        chooseone(ret, keys, ["gallery", "videos"]);
    } else {
        ret = Mock.Handler._object(options);
    }
    return ret;
};

let arr = [
    {
        name: "全家便利店",
        'create_time': (function() {
            return Random.time('yyyy-MM-dd HH:mm:ss');
        })(),
        content: {
            name: "付款金额", 
            value: "¥42.00",  
            type:"normel"
        },
        states: [
            {
                name: "附加信息",
                content: "微信支付到店红包¥0.15",
                type:"warning"
            }
        ]
    },
    {
        name: "退款到账通知",
        'create_time': (function() {
            return Random.time('yyyy-MM-dd HH:mm:ss');
        })(),
        content: {
            name: "退款金额", 
            value: "¥42.00",  
            type:"normal"
        },       
        states: [
            {
                name: "商品详情",
                content: "美团订单－201712345678123456781234",
                type:"normal"
            },
            {
                name: "商户名称",
                content: "大众点评",
                type:"normal"
            },
            {
                name: "退款方式",
                content: "退回零钱",
                type:"warning"
            },
            {
                name: "到账时间",
                content: "2018-03-12 08:59:33",
                type:"normal"
            },
            {
                name: "交易单号",
                content: "201712345678123456781234",
                type:"normal"
            },                                  
        ]
    },
    {
        name: "红包退回通知",
        'create_time': (function() {
            return Random.time('yyyy-MM-dd HH:mm:ss');
        })(),
        content: {
            name: "退款金额", 
            value: "¥42.00",  
            type:"normal"
        },
        states: [
            {
                name: "退款方式",
                content: "退回零钱",
                type:"warning"
            },
            {
                name: "到账时间",
                content: "2018-03-12 08:59:33",
                type:"normal"
            },
            {
                name: "退款原因",
                content: "红包超过24小时未被领取",
                type:"normal"
            },                                  
        ]
    },
    {
        name: "商家付款入账通知",
        'create_time': (function() {
            return Random.time('yyyy-MM-dd HH:mm:ss');
        })(),
        content: {
            name: "入账金额", 
            value: "¥42.00",  
            type:"normal"
        },
        states: [
            {
                name: "入账账户",
                content: "零钱",
                type:"normal"
            },
            {
                name: "付款商户",
                content: "包你说",
                type:"normal"
            },
            {
                name: "入账详情",
                content: "包你懂提现",
                type:"normal"
            },                                  
        ]
    }      
]

let $index = 0;
function createArrayite(options) {
    let ret = arr[$index];
    $index++;
    return ret;
}


let template = {
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    'list|4': [createArrayite]
};

mockupbuild(template, `./data/${basename}.json`);